﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DotSpatial.Map;
using DotSpatial.Drawing;
using HuanGis.selDefMapFunctions;

using System.Windows.Forms;



namespace HuanGis
{
   public  class MFManager
    {

       public MFManager(IMap map,FLayerManager p)
       {
           _geoMap = map;
           this.Parent = p;
 
       }

       protected FLayerManager Parent;


       private IMap _geoMap;

       private IFeatureLayer _activeLayer;

       SquareMF m_Square;

       RectangleMF m_Rectangle;

       CircleMF m_Circle;

       AddShapeMF m_Shape;

       SelectShapeMF m_SelectShape;

       MoveShapeMF m_MoveShape;


       string m_OptLayerName;

   /// <summary>
       /// 指示调用哪一个辅助绘制的功能方法
   /// </summary>
   /// <param name="selfunc"></param>
   /// <param name="lname">层名称</param>
       public void UseFunction( string  lname,SFunctionEnum selfunc)
       {
           if (m_SelectShape != null)
               m_SelectShape.ShapeRemoveHighlight(); //要先把上一个图层选中的Shape恢复原来的类型

           IFeatureLayer layer = this.Parent.LayerSystem.GetFeatureLayer(lname );
           _activeLayer = layer;

           m_OptLayerName = lname;

           switch (selfunc )
           {
               case SFunctionEnum.SQUARE:
                   this.UseSquareMF();
                   break;
               case SFunctionEnum.CIRCLE:

                   this.UseCircleMF();

                   break;
               case SFunctionEnum.RECTANGLE:

                   this.UseRectangleMF();

                   break;
               case SFunctionEnum.lINE :
                   this.UseShapeMF();
                   break;
               case SFunctionEnum.POLYGON:
                   this.UseShapeMF();
                   break;
               case SFunctionEnum.POINT :
                   this.UseShapeMF();
                   break;


                   ///下面是一些特殊的控制器


               case SFunctionEnum.SELECT :
                   this.UseSelectMF();
                   break;

               case SFunctionEnum .MOVE :


                   this.UseMoveMF(this.Parent .LayerSystem .GetEditableFeatureLayers ());

                   break;



               default:
                   break;
           }

 
       }

     


       protected void UseRectangleMF()
       {
           if (_geoMap == null) return;
           if (_activeLayer == null) return;
           if (m_Rectangle == null)
           {
               m_Rectangle = new RectangleMF (_geoMap);
               m_Rectangle.Name = "Rectangle";
               m_Rectangle.Parent = this;

              
           }
           if (_geoMap.MapFunctions.ContainsValue(m_Rectangle) == false)
           {
               _geoMap.MapFunctions.Add(m_Rectangle.Name, m_Rectangle);
           }
           _geoMap.FunctionMode = FunctionModes.None;
           _geoMap.Cursor = Cursors.Hand ;
          /// _geoMap.MapFrame.LayerSelected += MapFrame_LayerSelected;

           m_Rectangle.FeatureSet  = _activeLayer.DataSet ;
           m_Rectangle.Activate();
       }

       

       protected void UseCircleMF()
       {
           if (_geoMap == null) return;
           if (_activeLayer == null) return;
           if (m_Circle == null)
           {
               m_Circle = new CircleMF(_geoMap);
               m_Circle.Name = "Circle";
               m_Circle.Parent = this;

              
               
           }
           if (_geoMap.MapFunctions.ContainsValue(m_Circle) == false)
           {
               _geoMap.MapFunctions.Add(m_Circle.Name, m_Circle);
           }
           _geoMap.FunctionMode = FunctionModes.None;
           _geoMap.Cursor = Cursors.Hand;
           /// _geoMap.MapFrame.LayerSelected += MapFrame_LayerSelected;

           m_Circle.FeatureSet = _activeLayer.DataSet;
           m_Circle.Activate();
       
       
       }

       protected void UseSquareMF()
       {
           if (_geoMap == null) return;
           if (_activeLayer == null) return;
           if (m_Square == null)
           {
               m_Square = new SquareMF(_geoMap);
               m_Square.Name = "Square";
               m_Square.Parent = this;

              
           }
           if (_geoMap.MapFunctions.ContainsValue(m_Square) == false)
           {
               _geoMap.MapFunctions.Add(m_Square.Name, m_Square);
           }
           _geoMap.FunctionMode = FunctionModes.None;
           _geoMap.Cursor = Cursors.Hand;
           /// _geoMap.MapFrame.LayerSelected += MapFrame_LayerSelected;

           m_Square.FeatureSet = _activeLayer.DataSet;
           m_Square.Activate();
       
       
       
       }


       protected void UseShapeMF()
       {
           if (_geoMap == null) return;
           if (_activeLayer == null) return;
           if (m_Shape == null)
           {
               m_Shape = new AddShapeMF(_geoMap);
               m_Shape.Name = "Shape";
               m_Shape.Parent = this;

              
           }
           if (_geoMap.MapFunctions.ContainsValue(m_Shape) == false)
           {
               _geoMap.MapFunctions.Add(m_Shape.Name, m_Shape);
           }
           _geoMap.FunctionMode = FunctionModes.None;
           _geoMap.Cursor = Cursors.Hand;
           /// _geoMap.MapFrame.LayerSelected += MapFrame_LayerSelected;

           m_Shape.FeatureSet = _activeLayer.DataSet;
           m_Shape.Activate();
       }
            

       protected void UseSelectMF()
       {
           if (_geoMap == null) return;
           if (_activeLayer == null) return;
           if (m_SelectShape == null)
           {
               m_SelectShape = new  SelectShapeMF(_geoMap);
               m_SelectShape.Name = "SelectShape";
               m_SelectShape.Parent = this;

           }
           if (_geoMap.MapFunctions.ContainsValue(m_SelectShape) == false)
           {
               _geoMap.MapFunctions.Add(m_SelectShape.Name, m_SelectShape);
           }
           _geoMap.FunctionMode = FunctionModes.None;
           _geoMap.Cursor = Cursors.Hand;
           /// _geoMap.MapFrame.LayerSelected += MapFrame_LayerSelected;
           m_SelectShape.Layer = _activeLayer;
           m_SelectShape.Activate();
       
       
       }

       public void UseMoveMF(List<IFeatureLayer> fl)
       {
           if (_geoMap == null) return;
           if (fl == null) return;
           if (m_MoveShape == null)
           {
               m_MoveShape = new  MoveShapeMF(_geoMap);
               m_MoveShape.Name = "MoveShape";
               m_MoveShape.Parent = this;
           }
           if (_geoMap.MapFunctions.ContainsValue(m_MoveShape) == false)
           {
               _geoMap.MapFunctions.Add(m_MoveShape.Name, m_MoveShape);
           }
           _geoMap.FunctionMode = FunctionModes.None;
           _geoMap.Cursor = Cursors.Hand;
           /// _geoMap.MapFrame.LayerSelected += MapFrame_LayerSelected;
           m_MoveShape.Layer = fl;
           m_MoveShape.Activate();
       
       }


       protected void UseMoveVertexMF()
       {
           //if (_geoMap == null) return;
           //if (_activeLayer == null) return;
           //if (_moveVertexFunction == null)
           //{
           //    _moveVertexFunction = new MoveVertexFunction(_geoMap);
           //    _moveVertexFunction.Name = "MoveVertex";
           //}
           //if (_geoMap.MapFunctions.ContainsValue(_moveVertexFunction) == false)
           //{
           //    _geoMap.MapFunctions.Add(_moveVertexFunction.Name, _moveVertexFunction);
           //}
           //_geoMap.FunctionMode = FunctionModes.None;
           //_geoMap.Cursor = Cursors.Cross;
           //_geoMap.MapFrame.LayerSelected += MapFrame_LayerSelected;
           //_moveVertexFunction.Layer = _activeLayer;
           //_moveVertexFunction.Activate();
       }



       #region 对外编辑事件，以及对应的处理函数

       public event ShapeHandler ShapeAdded;

       public void AskShapeAdd(FShape shp)
       {

           if (ShapeAdded != null)
           {
               shp.LayerName = this.m_OptLayerName;
               ShapeAdded(shp);
           }


       }


       /// <summary>
       /// 对应有移动，和变更点位置
       /// </summary>
       public event ShapeHandler ShapeChanged;

       public void AskShapeChanged(FShape shp)
       {
           if (ShapeChanged !=null )
           {
               shp.LayerName = this.m_OptLayerName;
               ShapeChanged(shp);
               
           }
 
       }


       public event ShapeHandler ShapeRemoved;

       public void AskShapeRemoved(FShape shp)
       {

           if (ShapeRemoved !=null )
           {
               shp.LayerName = this.m_OptLayerName;
               ShapeRemoved(shp);
               
           }
       }




       //public event ShapeMoveHandler ShapeMoved;

       //public void AskMoveShape(string id,double xPre,double yPre)
       //{
       //    if (ShapeMoved !=null )
       //    {

       //        ShapeMoved(id,xPre ,yPre );
               
       //    }
 
       //}

        #endregion


    }
}
